home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Advanced I⁄O v2.3 / Advanced i⁄o / vhistogram.cc < prev    next >
C/C++ Source or Header  |  1995-05-07  |  2KB  |  59 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. //        Verification of histogram operations
  3.  
  4. #include "histogram.h"
  5. #include "myenv.h"
  6. #include <iostream.h>
  7.  
  8.  
  9.                 // Test getting and putting symbols
  10. static void test_getput(const int lwb, const int upb)
  11. {
  12.   cout << "\nTesting putting and getting symbols from [" << lwb << "," << upb
  13.        << "]" << endl;
  14.   Histogram histogram(lwb,upb);
  15.   register int i;
  16.   for(i=lwb; i<=upb; i++)
  17.     histogram.put(i);
  18.   for(i=lwb-1; i<=upb+1; i++)
  19.     histogram.put_coerce(i);
  20.   assert( histogram.get(histogram.symbol_lwb) == 3 ); // because of coercion
  21.   assert( histogram.get(histogram.symbol_upb) == 3 ); // because of coercion
  22.   for(i=lwb+1; i<=upb-1; i++)
  23.     assert( histogram.get(i) == 2 );
  24.   
  25.   cout << "\nDone\n";
  26. }
  27.  
  28.                 // Test computing 0th order entropy of the
  29.                 // distribution
  30. static void test_entropy(const int no_bits_expected, const int lwb)
  31. {
  32.   const int no_symbols = 1<<no_bits_expected;
  33.   const int symbol_count = 10;
  34.   cout << "\nCheck to see that the uniform distribution of " << no_symbols
  35.        << " symbols\ntakes exactly " << no_bits_expected  
  36.        << " bits per symbol to represent" << endl;
  37.   Histogram histogram(lwb,2*no_symbols+lwb);
  38.   for(register int i=0; i<symbol_count; i++)
  39.      for(register int j=0; j<no_symbols; j++)
  40.         histogram.put(2*j+lwb);
  41.   assert( histogram.no_distinct_symbols() == no_symbols );
  42.   cout << "\testimated entropy " << histogram.estimate_entropy() << endl;
  43.   assert( (int)(histogram.estimate_entropy() + 0.001) == no_bits_expected );
  44.  
  45.   if( histogram.no_distinct_symbols() <= 20 )
  46.     histogram.print("sample histogram");
  47.  
  48.   cout << "\nDone\n";
  49. }
  50.  
  51.  
  52.                 // Testing routing function
  53. main(void)
  54. {
  55.   test_getput(1,2);
  56.   test_getput(-1000,4000);
  57.   test_entropy(4,100);
  58.   test_entropy(14,-401);
  59. }